package com.example.cityiot;

import android.content.pm.ActivityInfo;
import android.os.Bundle;

import com.example.cityiot.adapter.LeftMenuAdapter;
import com.example.cityiot.fragment.FifthFragment;
import com.example.cityiot.fragment.FirstFragment;
import com.example.cityiot.fragment.FourthFragment;
import com.example.cityiot.fragment.SecondFragment;
import com.example.cityiot.fragment.ThirdFragment;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.Gravity;
import android.view.View;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    DrawerLayout drawerLayout;
    ImageButton mainIBMenu;
    TextView mainTVTitle;
    ListView mainLVMenuList;

    List<String> menuNameList = Arrays.asList(
            "第4题",
            "第9题",
            "第16题",
            "第32题",
            "第34题"
    );
    List<Fragment> fragmentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        init();
    }

    public void init(){
        drawerLayout = findViewById(R.id.drawerLayout);
        mainIBMenu = findViewById(R.id.main_ib_menu);
        mainTVTitle = findViewById(R.id.main_tv_title);
        mainLVMenuList = findViewById(R.id.main_lv_menu_list);
        fragmentList = new ArrayList<>();
        fragmentList.add(new FirstFragment());
        fragmentList.add(new SecondFragment());
        fragmentList.add(new ThirdFragment());
        fragmentList.add(new FourthFragment());
        fragmentList.add(new FifthFragment());

        mainTVTitle.setText(menuNameList.get(0));
        mainLVMenuList.setAdapter(new LeftMenuAdapter(this,R.layout.menu_list_item,menuNameList));
        mainIBMenu.setOnClickListener(this);
        mainLVMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,fragmentList.get(i)).commitAllowingStateLoss();
                mainTVTitle.setText(menuNameList.get(i));
                drawerLayout.closeDrawer(Gravity.LEFT);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_ib_menu:
                Log.d("MainActivity","11");
                drawerLayout.openDrawer(GravityCompat.START);
                break;
        }
    }
}